package com.google.gaedlab;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

public class QrService {
	public static Entity add(String text) {
		DatastoreService service = DatastoreServiceFactory.getDatastoreService();
		Entity e = new Entity("QR");
		e.setProperty("text", new Text(text));
		service.put(e);
		return e;
	}

	public static Entity find(String sid) {
		Long id = null;
		try {
			id = Long.parseLong(sid);
		} catch (NumberFormatException e) {
		}
		if (id == null) {
			return null;
		}
		DatastoreService service = DatastoreServiceFactory.getDatastoreService();
		try {
			return service.get(KeyFactory.createKey("QR", id));
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
